﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LayerEntity.Sistema;
using DevExpress.XtraGrid.Views.Grid;

namespace System2012.Almacen.Consultas
{
    public partial class frmListadoProductos : DevExpress.XtraEditors.XtraForm
    {
        DataSet DsRel = new DataSet();
        DataTable dtProd = new DataTable("productos");
        DataTable dtPrec = new DataTable("precios");
        public frmListadoProductos()
        {
            InitializeComponent();
          //  gridControl1.DataSource = CreateData();
            if (!BGWListar.IsBusy) BGWListar.RunWorkerAsync();
        }

        private void BGWListar_DoWork(object sender, DoWorkEventArgs e)
        {
            ESistema obj= new  ESistema ();
            StringBuilder cadena = new StringBuilder();
            cadena.Append("where (producto|| id_producto || familia) ILIKE '%");
            cadena.Append(txtFiltro.Text);
            cadena.Append("%'");
            obj.Condicion = cadena.ToString();
            e.Result=System2012.SystemUtils.RecodManajerproducto.GetListadoProducto(obj);
        }

        private void BGWListar_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            dtProd = (DataTable)e.Result;

            gridControl1.DataSource = dtProd;
          //  gridControl1.ForceInitialize();
                
        }


        DataTable CreateData()
        {
            DataTable dtm = new DataTable();
            DataTable dtd = new DataTable();
            DataSet ds = new DataSet();
            ESistema obj = new ESistema();
            dtm = System2012.SystemUtils.RecodManajerproducto.GetListadoProducto(obj);
            dtd = System2012.SystemUtils.RecodManajerproducto.GetListadoProducto(obj);
            ds.Tables.AddRange(new DataTable[] { dtm, dtd });
            ds.Relations.Add("Precios", dtm.Columns["id_producto"], dtd.Columns["id_producto"]);
            return dtm;
        }

        void CreateDetailTree()
        {
            GridView gvdt = new GridView(gridControl1);
            gvdt.Columns.AddVisible("Name");
           // gridControl1.LevelTree.Nodes.Add("gridPrecios", gvdt);
            gridControl1.LevelTree.Nodes[1].LevelTemplate = gvdt;
        }

        private void txtFiltro_TextChanged(object sender, EventArgs e)
        {
            if (!BGWListar.IsBusy) BGWListar.RunWorkerAsync();
        }

        private void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            GridView view = sender as GridView;
            if (e.Column.FieldName == "costo_almacen")
            {
                e.Appearance.BackColor = Color.Red;
                e.Appearance.ForeColor = Color.Snow;
            }
            if (e.Column.FieldName == "precio1")
            {
                e.Appearance.BackColor = Color.Green;
                e.Appearance.ForeColor = Color.Snow;
            }
        }
    }
}